// pages/circle/index.js
var common = require('../../lib/common.js');
var url = require('../../config/url.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 菜单
    menuData: [],
    // 搜索词
    searchValue: "",
    // 活动
    active: 0,
    // 圈子
    circles: [],
    // 产品
    products: [],
    // 分页
    pages: [],
    // 加载
    loadding: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getMenus();
  },

  onPullDownRefresh() {
    this.setData({
      // 菜单
      menuData: [],
      // 搜索词
      searchValue: "",
      // 活动
      active: -1,
      // 圈子
      circles: [],
      // 产品
      products: [],
      // 分页
      pages: [],
      // 加载
      loadding: [],
    });
    this.getMenus();
    wx.stopPullDownRefresh();
  },


  /**
   * 获取菜单
   */
  getMenus() {
    common.post(url.getCircleOneType, {}, (data) => {
      let circles = [[]];
      let pages = [1];
      let loadding = [false];
      let products = [[]];

      for (let i in data) {
        data[i].url = '/pages/circle_type/index?id=' + data[i].id + '&name=' + data[i].name;
        circles.push([]);
        products.push([]);
        pages.push(1);
        loadding.push(false);
      }
      data.splice(0, 0, {
        name: '全部',
        id: -1,
      });

      this.setData({ menuData: data, circles, pages, loadding, products, active: 0 });
      this.getCircleList(0);
      this.getProductList(0);
    });
  },

  /**
     * 选项
     */
  onOptionChange({ detail }) {
    let active = this.data.active;
    active = detail.index;
    this.setData({ active });

    if (this.data.pages[active] > 1 || this.data.pages[active] === false) {
      return;
    }

    this.getCircleList(active);
    this.getProductList(active);
  },

  /**
   * 获取圈子列表
   */
  getCircleList(index) {
    common.post(url.getCircleListByType, { type_id: this.data.menuData[index].id, name: this.data.searchValue, limit: 50 }, (data) => {
      let circles = this.data.circles;
      circles[index] = data;

      this.setData({ circles });
    })
  },

  /**
   * 获取产品列表
   */
  getProductList(index) {
    let limit = 10;
    let page = this.data.pages[index];
    if (page === false || this.data.loadding[index] === true) return;

    // 设置加载态
    let loadding = this.data.loadding;
    loadding[index] = true;
    this.setData({ loadding });

    common.post(url.getCirclePhotoListByType, { type_id: this.data.menuData[index].id, name: this.data.searchValue, limit: limit, page: page }, (data) => {
      let products = this.data.products;
      products[index] = products[index].concat(data);

      let pages = this.data.pages
      if (data.length == 0) {
        pages[index] = false;
      } else {
        pages[index]++;
      }

      loadding[index] = false;
      this.setData({ products, pages, loadding });
    })
  },

  onReachBottom() {
    if (this.data.active === -1) return;
    this.getProductList(this.data.active);
  },

  onTapSearch() {
    this.setData({ active: 0 });
    let circles = this.data.circles;
    let products = this.data.products;
    let pages = this.data.pages;
    let loadding = this.data.loadding;


    for (let i = 0; i < circles.length; i++) {
      circles[i] = [];
      products[i] = [];
      pages[i] = 1;
      loadding[i] = false;
    }
    this.setData({ circles, products, pages, loadding });

    this.getCircleList(0);
    this.getProductList(0);
  },

  bindSearchInput: function (e) {
    this.setData({
      searchValue: e.detail.value
    })
    console.log(e.detail.value);
  },

  onJoinCircle() {
    wx.navigateTo({
      url: '/pages/circle_join/index'
    });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})